package com.example.jwtauthdemo.service.impl;

import com.example.jwtauthdemo.mapper.OrderMapper;
import com.example.jwtauthdemo.mapper.UserMapper;
import com.example.jwtauthdemo.model.Order;
import com.example.jwtauthdemo.model.User;
import com.example.jwtauthdemo.service.OrderService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;
    private final UserMapper userMapper;

    public OrderServiceImpl(OrderMapper orderMapper, UserMapper userMapper) {
        this.orderMapper = orderMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<Order> getOrdersByCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        User user = userMapper.findByUsername(username)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
        
        return orderMapper.findByUserId(user.getId().intValue());
    }
}